import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

class MySwiper extends StatelessWidget {
  const MySwiper({Key? key, required this.swiperList, required this.defalutStyle}) : super(key: key);
  final List swiperList;
  final bool defalutStyle;

  @override
  Widget build(BuildContext context) {
    return Swiper(
      // 轮播总数
      itemCount: swiperList.length,
      // 轮播项
      itemBuilder: (BuildContext context, int index) {
        return Tooltip( // 长按提示
          message: swiperList[index].title,
          child: Image(
            image: AssetImage(swiperList[index].path),
            fit: BoxFit.fill,
          ),
        );
      },
      // 分页器插件
      pagination: SwiperPagination(
        alignment: defalutStyle?Alignment.bottomCenter:Alignment.bottomRight,
        margin: defalutStyle ? const EdgeInsets.only(bottom: 20.0) : const EdgeInsets.fromLTRB(0.0, 0.0, 20.0, 20.0) ,
        builder: defalutStyle?
          const DotSwiperPaginationBuilder(
            color: Colors.black54,
            activeColor: Colors.green,
          ) : SwiperCustomPagination(
            builder: (BuildContext context, SwiperPluginConfig config) {
              return CustomPoint(currentIndex: config.activeIndex, length: swiperList.length);
            },
          ),
        // 圆点插件
        // builder: DotSwiperPaginationBuilder(
        //   color: Colors.black54,
        //   activeColor: Colors.green,
        // ),
        // 自定义指示器
        // builder: SwiperCustomPagination(
        //   builder: (BuildContext context, SwiperPluginConfig config) {
        //     return CustomPoint(currentIndex: config.activeIndex, length: swiperList.length);
        //   },
        // )
      ),
      // 滚动方向
      scrollDirection: Axis.horizontal,
      // 是否自动滚动
      autoplay: true,
      // 用户操作时停止自动轮播
      autoplayDisableOnInteraction: true,
      // 无限轮播
      loop: true,
      // 点击某一项
      onTap: (index) {},
      // control: const SwiperControl(
      //     size: 15.0,
      //     color: Colors.grey
      // ),
      viewportFraction: 1,
      // 缩放
      scale: 1,
    );
  }
}

//自定义指示器点样式
class CustomPoint extends StatelessWidget {
  const CustomPoint({Key? key, required this.currentIndex, required this.length}) : super(key: key);
  final int currentIndex;
  final int length;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: length*25,
      child: GridView.builder(
        itemCount: length,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        scrollDirection:Axis.vertical,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: length,
          childAspectRatio:5,
          mainAxisSpacing: 0,
          crossAxisSpacing:5
        ),
        itemBuilder: (BuildContext context, int index){
          return Container(
            decoration: BoxDecoration(
              color:currentIndex == index ? Theme.of(context).primaryColor : Colors.white,
              borderRadius: BorderRadius.circular(10)
            ),
          );
        },
      ),
    );
  }
}

